<?php

namespace app\backstage\validate;

use think\Validate;

class RechargeValidate extends Validate
{
    protected $rule = [
        'id' => 'number|gt:0',
        'total_fee' => 'require|checkFee|gt:0',
        'lable_open' => 'require|in:0,1',
        'lable' => 'max:4',
        'virtual_currency' => 'require|number|egt:0',
        'point_gift' => 'require|number|egt:0',
        'vipdays' => 'require|number|egt:0',
        'first' => 'require|in:0,1',
        'selected' => 'require|in:0,1',
        'state' => 'require|in:0,1',
        'sort' => 'require|number|egt:0',

        'title' => 'require|max:40',
        'img_url' => 'require',
        'pop_img' => 'require',

        'desc' => 'max:40',
        'name' => 'require',
    ];

    protected $message = [
        'total_fee.require' => '充值金额必填',
        'total_fee.gt' => '充值金额必须大于0',
        'lable' => '角标文字最多4位',
        'virtual_currency' => '书币值异常',
        'point_gift' => '书点值异常',
        'vipdays' => '会员天数值异常',
        'sort' => '排序值异常',
        'img_url' => '请上传图片',
        'pop_img' => '请上传弹窗图片',
        'end_time.egt' => '结束时间必须大于开始时间',
    ];

    protected $scene = [
        'setItem' => ['id', 'total_fee', 'lable_open', 'lable', 'virtual_currency', 'point_gift', 'vipdays', 'first', 'selected', 'state', 'sort', 'desc'],
        'setActivity' => ['id', 'title', 'img_url', 'total_fee', 'virtual_currency', 'point_gift', 'vipdays', 'pop_img'],
        'addTemplate' => ['name'],
        'delTemplate' => ['id'],
    ];


    protected function checkFee($value, $rule, $data = [])
    {
        $pattern = '/^\d+(\.\d{1})?$/';
        if (preg_match($pattern, $value) !== 1) {
            return '参数异常，金额必须是数字，并且最多只能存在小数点后一位';
        }
        return true;
    }
}